package com.dp.appi.controller;

import com.dp.appi.entity.UserEntity;
import com.dp.appi.service.ExpPhotoService;
import com.dp.appi.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * Created by xiaoji on 2016/5/12.
 * 踏勘拍照
 */
@Controller
@RequestMapping(value = "/expPhotoControl")
public class ExpPhotoControl {
    private static Logger log = LoggerFactory.getLogger(ExpPhotoControl.class);
    @Autowired
    private ExpPhotoService expPhotoService;

    @Value("${file_path}")
    private String file_path;

    /**
     * 查询踏勘照片
     * @param modelMap
     * @param param
     * @param exploration_id 踏勘id
     */
    @RequestMapping(value = "/expPhotoList",method = RequestMethod.GET)
    public void expPhotoList(ModelMap modelMap,@RequestParam Map<String,String> param,Long exploration_id) {
        try {
            modelMap.addAllAttributes(expPhotoService.expPhotoList(exploration_id));
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
            e.printStackTrace();
        }
    }

    /**
     * 保存踏勘照片
     * @param modelMap
     * @param param
     * @param exploration_id
     */
    @RequestMapping(value = "/saveExpPhoto",method = RequestMethod.POST)
    public void saveExpPhoto(ModelMap modelMap,@RequestParam Map<String,String> param,
                             Long exploration_id,Integer photo_type,
                             HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        try {
            String type = request.getParameter("type");//上传的文件类型
            String _path = "";
            if (Constant.HEADER_TYPE.equals(type)) {
                _path += "header/";
            } else if (Constant.PROJECT_TYPE.equals(type)) {
                _path += "project/";
            } else if (Constant.RECON_FIRST_TYPE.equals(type)) {
                _path += "stage_"+exploration_id+"/recon_first/";
            } else if (Constant.RECON_SECOND_TYPE.equals(type)) {
                _path += "stage_"+exploration_id+"/recon_second/";
            } else if (Constant.RECON_THIRD_TYPE.equals(type)) {
                _path += "stage_"+exploration_id+"/recon_third/";
            } else {
                _path += "pic/";
            }
            _path=_path+ DateUtil.getNowDateStr()+"/";

            File f = new File(file_path + _path);
            if (!f.exists()) {
                f.mkdirs();
            }

            String filename = file.getOriginalFilename();

            String[] _files = filename.split("\\.");
            String suffix = _files[1];
            long ss=System.currentTimeMillis();
//            _path += ss + "." + suffix;
            String _path2=_path + ss+ "." + suffix;

            String path = file_path + _path2;
            File targetFile = new File(path);

            file.transferTo(targetFile);

            String _paththumb=_path + ss+"_300x300"+ "." + suffix;
            String thumbPath = file_path + _paththumb;//缩略图路径
            //开始切图
            ImageUtils.scale2(path, thumbPath, 300, 300, true);
            modelMap.addAttribute("photo_url", "upload/" + _path2);
            modelMap.addAttribute("photo_thumb", "upload/" + _paththumb);
            modelMap.addAttribute("photo_id",expPhotoService.saveExpPhoto("upload/" + _path2,exploration_id,
                    photo_type,"upload/" + _paththumb));
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
            e.printStackTrace();
        }
    }

    /**
     * 删除踏勘照片
     * @param modelMap
     * @param param
     * @param photo_id 拍照Id
     */
    @RequestMapping(value = "/delExpPhoto",method = RequestMethod.POST)
    public void delExpPhoto(ModelMap modelMap,@RequestParam Map<String,String> param,Long photo_id) {
        try {
            expPhotoService.delExpPhoto(photo_id);
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
            e.printStackTrace();
        }
    }
}
